package org.hackystat.sensor.manual.devtime;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;

/**
 * The table model to display sensor data.
 * @author Shaoxuan Zhang
 *
 */
public class SensorDataTableModel extends AbstractTableModel {
  
  /** Support serialization. */
  private static final long serialVersionUID = -386238264456193685L;
  
  private List<SensorData> sensorData = new ArrayList<SensorData>();

  /** The time format.*/
  private final DateFormat timeFormat = 
    DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
  
  /**
   * @return the number of column in this table model.
   */
  public int getColumnCount() {
    return 3;
  }

  /**
   * @return the number of row in this table model.
   */
  public int getRowCount() {
    return sensorData.size();
  }
  
  /**
   * @param columnIndex the index.
   * @return the name of the column with given index.
   */
  @Override
  public String getColumnName(int columnIndex) {
    switch (columnIndex) {
    case 0: return "Tool";
    case 1: return "Resource";
    case 2: return "Timestamp";
    default: return "";
    }
  }

  /**
   * @param row row index.
   * @param column column index.
   * @return the value at the given cell.
   */
  public Object getValueAt(int row, int column) {
    SensorData data = sensorData.get(row);
    //System.out.println("getting value at (" + row + "," + column + ")");
    switch (column) {
    case 0: return data.getTool();
    case 1: return data.getResource();
    case 2: return timeFormat.format(data.getTimestamp().toGregorianCalendar().getTime());
    default: return "";
    }
  }

  /**
   * @return the sensorData
   */
  public List<SensorData> getSensorData() {
    return sensorData;
  }

  /**
   * Remove data of the selected indices.
   * @param selectedIndices the selected indices.
   * @return the list of data removed.
   */
  public List<SensorData> removeData(int[] selectedIndices) {
    //System.out.println("Removing " + selectedIndices.length + " data.");
    List<SensorData> removeData = new ArrayList<SensorData>();
    for (int i : selectedIndices) {
      removeData.add(sensorData.get(i));
    }
    sensorData.removeAll(removeData);
    return removeData;
  }

}
